package com.ae.shield.common.events;

import com.ae.shield.ModLib;
import com.ae.shield.api.IEnergyItem;
import com.ae.shield.api.IStorageItem;
import com.ae.shield.common.capability.Itemhandler.ProItemStackHandler;
import com.ae.shield.common.capability.energy.ProEnergyStorage;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = ModLib.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class AttachCapEvent {
    @SubscribeEvent
    public static void itemEnergyCap(AttachCapabilitiesEvent<ItemStack> event){
        ItemStack stack = event.getObject();
        Item item = stack.getItem();

        if (item instanceof IEnergyItem){
            IEnergyItem iEnergyItem = (IEnergyItem) item;
            event.addCapability(new ResourceLocation(ModLib.MOD_ID, "energy"), new ProEnergyStorage(stack, iEnergyItem.getEnergyCapacity(), iEnergyItem.getMaxExtract(), iEnergyItem.getMaxReceive()));
        }

        if (item instanceof IStorageItem){
            event.addCapability(new ResourceLocation(ModLib.MOD_ID, "items"), new ProItemStackHandler(((IStorageItem) stack.getItem()).getSlots(), stack));
        }
    }
}
